package com.src.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.src.myapplication.base.BaseActivity;
import com.src.myapplication.utils.FragmentFactoryJieMian;
import com.src.myapplication.widget.crouton.Crouton;
import com.src.myapplication.widget.crouton.Style;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.bt1)
    RadioButton bt1;
    @BindView(R.id.bt2)
    RadioButton bt2;
    @BindView(R.id.bt3)
    RadioButton bt3;
    @BindView(R.id.bt4)
    RadioButton bt4;
    @BindView(R.id.bt5)
    RadioButton bt5;
    @BindView(R.id.rg)
    RadioGroup rg;
    private RadioButton[] mButtons;
    private int currentIndex = 0;
    private static boolean isExit = false;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    private List<Fragment> mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        initData();
        getSupportFragmentManager().
                beginTransaction()
                .add(R.id.fl, mFragments.get(0))
                .commit();
    }

    @Override
    public int getLayoutId() {
        return (R.layout.activity_main);
    }

    private void initData() {
        mFragments = new ArrayList<>();
        mButtons = new RadioButton[rg.getChildCount()];
        for (int i = 0; i < rg.getChildCount(); i++) {
            mButtons[i] = (RadioButton) rg.getChildAt(i);
            Fragment fragment = FragmentFactoryJieMian.createFragment(i);
            mFragments.add(fragment);
        }
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < rg.getChildCount(); i++) {
                    if (mButtons[i].getId() == checkedId) {
                        switchFragment(i);
                    }
                }
            }
        });
    }

    /**
     * 每个fragment的之间的交换
     *
     * @param id
     */
    private void switchFragment(int id) {
        //目标fragment
        Fragment fragment = mFragments.get(id);
        //当前faagemnt
        Fragment fragment1 = mFragments.get(currentIndex);
        //开启一个事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment.isAdded()) {
            transaction.hide(fragment1).show(fragment);
        } else {
            transaction.hide(fragment1).add(R.id.fl, fragment);
        }
        transaction.commit();
        currentIndex = id;
    }

    //处理返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Crouton.makeText(this,"再按一次退出程序", Style.INFO).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }
}